<?php

class CommentMapper extends AbstractMapper
{

    protected function _init()
    {
	$this->setModel(new CommentModel());
	$this->setTableName(COMMENT_TABLE);
    }

    public function getByReviewId2($reviewId)
    {
	if (!$reviewId) {
	    return false;
	}

	return $this->getAllByQuery(array(
		    'select' => array('*'),
		    'from' => COMMENT_TABLE,
		    'where' => 'target_id = :target_id AND target_type = \'review\' AND `delete` = \'0\'',
		    'order' => 'id ASC',
		    'params' => array(':target_id' => $reviewId)
		));
    }
    
    public function getWishesComments()
    {
	return $this->getAllWithRelation(array(
		    'select' => array('c.*'),
		    'from' => 'comment AS c',
		    'join' => 'INNER JOIN user ON user.id=c.user_id',
		    'where' => 'c.target_type = :target_type',
		    'params' => array(':target_type' => 'wishes')
		    ), array('user'));
    }

    public function getByReviewId($reviewId)
    {	
	return $this->getAllWithRelation(array(
		    'select' => array('c.*'),
		    'from' => 'comment AS c',
		    'join' => 'INNER JOIN user ON user.id=c.user_id',
		    'where' => 'c.target_id = :target_id AND c.target_type = :target_type',
		    'params' => array(':target_id' => $reviewId, ':target_type' => 'review')
		    ), array('user'));
    }
    public function getByCompanyId($companyId)
    {
	return $this->getAllWithRelation(array(
		    'select' => array('c.*'),
		    'from' => 'comment AS c',
		    'join' => 'INNER JOIN user ON user.id=c.user_id',
		    'where' => 'c.target_id = :target_id AND c.target_type = :target_type',
		    'params' => array(':target_id' => $companyId, ':target_type' => 'company ')
		    ), array('user'));
    }
    public function getByUserId($id, $page = 1, $limit = 10)
    {
	if (!$id) {
	    return false;
	}
	$sql = $this->createSqlCommand()
		->select("*")
		->from($this->getTableName())
		->where("user_id = :user_id", array(':user_id' => $id))
		->limit($limit)
		->offset(($page - 1) * $limit);

	return $this->getObjectList($sql->queryAll());
    }

}
